package com.demo.quartz.scheduler;

import com.demo.quartz.jobs.DumbJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.concurrent.TimeUnit;

/**
 * @author chenx
 * @Description :
 * @create 2020-05-15 10:53
 */
public class DumbScheduler2 {
	public  static void main (String[] srgs) throws SchedulerException, InterruptedException {
	    //1.创建调度器
	    SchedulerFactory schedulerFactory = new StdSchedulerFactory();

	    Scheduler scheduler = schedulerFactory.getScheduler();
	    //2.创建jobDetail实例，与PrintWordsJob绑定
        JobDetail jobDetail = JobBuilder.newJob(DumbJob.class)
                .withIdentity("myJob", "group1") // name "myJob", group "group1"
                .usingJobData("jobSays", "Hello World!")
                .usingJobData("myFloatValue", 3.141f)
                .build();
	    //创建触发器trigger
        Trigger trigger =  TriggerBuilder.newTrigger()
                .withIdentity("dumbTrigger","triggerGroup2")
                .startNow()//立即执行
                .withSchedule(//CronScheduleBuilder.cronSchedule("* 30 10 ? * 1/5 2018")//cron日历调度
                        SimpleScheduleBuilder.simpleSchedule()
                                .withIntervalInSeconds(1)//1s执行一次
                                .repeatForever())//一直执行
                .build();

        scheduler.scheduleJob(jobDetail,trigger);
        System.out.println("***********start****************");
        scheduler.start();
        //5s后停止
        TimeUnit.SECONDS.sleep(5);
        scheduler.shutdown();
        System.out.println("$$$$$$$$$shut down$$$$$$$$$$$$$");

    }
}
